package home1ess.passport.dao.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import common.pojo.PermissionPO;
import home1ess.passport.dao.mapper.UserMapper;
import home1ess.passport.dao.repository.IUserRepository;
import home1ess.passport.pojo.po.UserPO;
import home1ess.passport.pojo.po.UserRegPO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Arrays;
import java.util.List;

@Repository
public class UserRepository implements IUserRepository {

    @Autowired
    private UserMapper userMapper;

    //根据手机号查询用户
    @Override
    public UserRegPO selectByPhoneNumber(String phoneNumber) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("phone_number",phoneNumber);
        UserRegPO userRegPO = userMapper.selectOne(queryWrapper);
        return userRegPO;
    }
    //保存用户
    @Override
    public Integer save(UserRegPO userRegPO) {
        int insert = userMapper.insert(userRegPO);
        return insert;
    }

    @Override
    public void updatePassword(UserRegPO userRegPO) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("phone_number",userRegPO.getPhoneNumber());
        userMapper.update(userRegPO,queryWrapper);
    }

    @Override
    public List<PermissionPO> getPermissionsByUserId(Long id) {
        //todo 模拟数据
        //从数据库查询当前登录用户的所有权限列表,4表连查,用mybatis 复杂sql写到xml
        PermissionPO articleAdd = new PermissionPO();
        articleAdd.setAuthority("/article/add");

        PermissionPO articleDetail = new PermissionPO();
        articleDetail.setAuthority("/article/detail");

        return Arrays.asList(articleAdd,articleDetail);
    }


    @Override
    public UserPO getUserInfoByUserName(String phoneNumber) {
        UserPO userPO = new UserPO();
        UserRegPO userRegPO = selectByPhoneNumber(phoneNumber);
        userPO.setPhoneNumber(userRegPO.getPhoneNumber());
        userPO.setPassword(userRegPO.getPassword());
        userPO.setId(userRegPO.getId());
        return userPO;
    }


}
